Skip to content

fix: skip redundant save on Entity.load()#3

Merged
deucalioncodes merged 1 commit intomainfrom
fix/skip-redundant-save-on-load
Mar 18, 2026
Merged

fix: skip redundant save on Entity.load()#3
deucalioncodes merged 1 commit intomainfrom
fix/skip-redundant-save-on-load

Conversation

@deucalioncodes
Copy link
Copy Markdown
Member

Summary

Every Entity.load() was triggering a full re-serialization and write-back via __init___save(), even though the data was just read from storage.

Before (per load)

storage.get → json.loads → __init__ → _save() → serialize() → json.dumps → storage.insert
  • json.loads (read)
  • serialize() (Python dict construction, MRO walk)
  • json.dumps (redundant write-back)
  • storage.insert (redundant StableBTreeMap write on canister)
  • 1× audit json.dumps (if audit enabled)

After (per load)

storage.get → json.loads → __init__ → done
  • json.loads (read)

Changes

  1. __init__: Skip _save() when _loaded=True — the data is already in storage.
  2. Entity.load(): Explicitly call _save() after migration, so migrated data is still persisted.

Test results

106 passed, 3 pre-existing failures (unrelated TestDatabase.setUp issue when run via pytest).

Closes #1

…on.dumps + storage write

Every Entity.load() was triggering a full re-serialization and write-back
via __init__ -> _save(), even though the data was just read from storage.

This caused 3 JSON round-trips + 1 storage write for every read-only load:
- 1x json.loads (read)
- 1x serialize() + json.dumps (redundant write-back)
- 1x storage.insert (redundant StableBTreeMap write on canister)
- 1x audit json.dumps (if audit enabled)

Fix: skip _save() in __init__ when _loaded=True. Explicitly save in
Entity.load() only when migration was applied, so migrated data is
still persisted.

Closes #1
@deucalioncodes deucalioncodes merged commit c1ca408 into main Mar 18, 2026
5 checks passed
@deucalioncodes deucalioncodes deleted the fix/skip-redundant-save-on-load branch March 18, 2026 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix redundant save on every Entity.load() — eliminates unnecessary json.dumps + storage write

1 participant